package dhm.com.hpx.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import dhm.com.hpx.R;
import dhm.com.hpx.entity.CuponBean;

/**
 * Created by admin
 * 2019/4/29
 * 所有订单适配器
 */
public class CuponAdapter extends RecyclerView.Adapter<CuponAdapter.ViewHolder> {

    private Context context;


    List<CuponBean.DataBean> list=new ArrayList<>();

    public CuponAdapter(Context context) {
        this.context = context;
    }

    public void setList(List<CuponBean.DataBean> list) {
        this.list = list;
        notifyDataSetChanged();
    }
    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        ViewHolder viewHolder;
        View view= LayoutInflater.from(context).inflate(R.layout.item_cupon, null);
        viewHolder=new ViewHolder(view);
        return viewHolder;
    }

    @SuppressLint("NewApi")
    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, final int i) {

        viewHolder.amount.setText("￥"+list.get(i).getAmount());
        viewHolder.with_amount.setText("满"+list.get(i).getWith_amount()+"使用");
        viewHolder.title.setText(list.get(i).getTitle());
        viewHolder.time.setText(list.get(i).getStart_time()+"——"+list.get(i).getEnd_time());

        switch (list.get(i).getStatus()){
            case 0:
                //未使用
                viewHolder.amount.setTextColor(context.getResources().getColor(R.color.main));
                viewHolder.with_amount.setTextColor(context.getResources().getColor(R.color.main));
                viewHolder.title.setTextColor(Color.parseColor("#222222"));
                viewHolder.time.setTextColor(Color.parseColor("#999999"));
                viewHolder.used.setVisibility(View.GONE);
                viewHolder.use.setVisibility(View.VISIBLE);
                viewHolder.use.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        onClick.item(i);
                    }
                });
                break;
            case 1:
                //已使用
                viewHolder.amount.setTextColor(Color.parseColor("#999999"));
                viewHolder.with_amount.setTextColor(Color.parseColor("#999999"));
                viewHolder.title.setTextColor(Color.parseColor("#999999"));
                viewHolder.time.setTextColor(Color.parseColor("#999999"));
                viewHolder.use.setVisibility(View.GONE);
                viewHolder.used.setVisibility(View.VISIBLE);
                viewHolder.used.setImageResource(R.mipmap.used);
                break;
            case 2:
                //已过期
                viewHolder.amount.setTextColor(Color.parseColor("#999999"));
                viewHolder.with_amount.setTextColor(Color.parseColor("#999999"));
                viewHolder.title.setTextColor(Color.parseColor("#999999"));
                viewHolder.time.setTextColor(Color.parseColor("#999999"));
                viewHolder.use.setVisibility(View.GONE);
                viewHolder.used.setVisibility(View.VISIBLE);
                viewHolder.used.setImageResource(R.mipmap.noused);
                break;
                default:
        }

    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        public TextView amount;
        public RelativeLayout rela;
        public TextView with_amount;
        public TextView title;
        public TextView time;
        public Button use;
        public ImageView used;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            rela=itemView.findViewById(R.id.rela);
            amount=itemView.findViewById(R.id.amount);
            with_amount=itemView.findViewById(R.id.with_amount);
            title=itemView.findViewById(R.id.title);
            time=itemView.findViewById(R.id.time);
            use=itemView.findViewById(R.id.use);
            used=itemView.findViewById(R.id.used);
        }
    }

    private OnClick onClick;
    public void setdeleteClick(OnClick onClick) {
        this.onClick = onClick;
    }
    public interface OnClick{
        void item(int posttion);
    }

    private OnIntentClick onIntentClick;
    public void setaddClick(OnIntentClick onClick) {
        this.onIntentClick = onClick;
    }
    public interface OnIntentClick{
        void item();
    }



}

